/**
 * 转正流程状态渲染对应组件
 */
import React from 'react';
import { Divider, Empty, message } from 'antd';
import { PageContainer } from '@/molecules';
import { useRequest, defaultFormatResult } from '@/hooks';
import { connect } from 'umi';
import { history } from 'umi'
import { sameTabProps } from '@/utils/tab';
import { PersonalCard } from '../containers';
import { getInformalDetailByCode } from '../services';
import { TRIAL_ACTION } from '../constants';

import GuideHR from './GuideHR';
import GuideTutor from './GuideTutor';
import GuideLeader from './GuideLeader';
import AssessTutorW from './AssessTutorW';
import AssessLeaderW from './AssessLeaderW';
import AssessHRW from './AssessHRW';
import AssessTutorM from './AssessTutorM';
import AssessLeaderM from './AssessLeaderM';
import AssessHRM from './AssessHRM';
import AssessTutorQ from './AssessTutorQ';
import AssessLeaderQ from './AssessLeaderQ';
import AccessHRQ from './AssessHRQ';
import InterviewPrepareW from './InterviewPrepareW';
import InterviewPrepareM from './InterviewPrepareM';
import InterviewPrepareQ from './InterviewPrepareQ';
import InterviewRecord from './InterviewRecord';
import InterviewSummary from './InterviewSummary';

const PAGE_INFO = {
    [TRIAL_ACTION.GUIDE_HR]: { component: GuideHR, title: '人事引导' },
    [TRIAL_ACTION.GUIDE_TEACHER]: { component: GuideTutor, title: '带教引导' },
    [TRIAL_ACTION.GUIDE_LEADER]: { component: GuideLeader, title: '主管引导' },
    [TRIAL_ACTION.ASSESS_TEACHER_WEEK]: { component: AssessTutorW, title: '带教人周评' },
    [TRIAL_ACTION.ASSESS_LEADER_WEEK]: { component: AssessLeaderW, title: '主管周评' },
    [TRIAL_ACTION.ASSESS_HR_WEEK]: { component: AssessHRW, title: '人事周评' },
    [TRIAL_ACTION.ASSESS_TEACHER_MONTH]: { component: AssessTutorM, title: '带教人月评' },
    [TRIAL_ACTION.ASSESS_LEADER_MONTH]: { component: AssessLeaderM, title: '主管月评' },
    [TRIAL_ACTION.ASSESS_HR_MONTH]: { component: AssessHRM, title: '人事月评' },
    [TRIAL_ACTION.ASSESS_TEACHER_QUARTER]: { component: AssessTutorQ, title: '带教人季评' },
    [TRIAL_ACTION.ASSESS_LEADER_QUARTER]: { component: AssessLeaderQ, title: '主管季评' },
    [TRIAL_ACTION.ASSESS_HR_QUARTER]: { component: AccessHRQ, title: '人事季评' },

    [TRIAL_ACTION.INTERVIEW_READY_WEEK]: { component: InterviewPrepareW, title: '面谈准备' },
    [TRIAL_ACTION.INTERVIEW_LOG_WEEK]: { component: InterviewRecord, title: '面谈记录' },
    [TRIAL_ACTION.INTERVIEW_SUMMARY_WEEK]: { component: InterviewSummary, title: '面谈总结' },

    [TRIAL_ACTION.INTERVIEW_READY_MONTH]: { component: InterviewPrepareM, title: '面谈准备' },
    [TRIAL_ACTION.INTERVIEW_LOG_MONTH]: { component: InterviewRecord, title: '面谈记录' },
    [TRIAL_ACTION.INTERVIEW_SUMMARY_MONTH]: { component: InterviewSummary, title: '面谈总结' },

    [TRIAL_ACTION.INTERVIEW_READY_QUARTER]: { component: InterviewPrepareQ, title: '面谈准备' },
    [TRIAL_ACTION.INTERVIEW_LOG_QUARTER]: { component: InterviewRecord, title: '面谈记录' },
    [TRIAL_ACTION.INTERVIEW_SUMMARY_QUARTER]: { component: InterviewSummary, title: '面谈总结' }
};

function Process({ dispatch, location, user = {} }) {
    const { query } = location;
    const { data, loading } = useRequest(
        () =>
            getInformalDetailByCode({
                employeeCode: query.employeeCode,
                status: query.status,
                assessId: query.assessId
            }),
        {
            ...defaultFormatResult
        }
    );

    // 优先取 URL 中的状态标识
    const status = query.status || data?.status;

    // 后台只有 准备中、待总结状态，“记录”的状态是虚拟的
    const interviewComplete = React.useCallback(
        (dataStatus, hasInterviewProcedure) => {
            const prepareStatusArr = [
                TRIAL_ACTION.INTERVIEW_READY_WEEK,
                TRIAL_ACTION.INTERVIEW_READY_MONTH,
                TRIAL_ACTION.INTERVIEW_READY_QUARTER
            ];
            const logStatusArr = [
                TRIAL_ACTION.INTERVIEW_LOG_WEEK,
                TRIAL_ACTION.INTERVIEW_LOG_MONTH,
                TRIAL_ACTION.INTERVIEW_LOG_QUARTER
            ];
            const summaryStatusArr = [
                TRIAL_ACTION.INTERVIEW_SUMMARY_WEEK,
                TRIAL_ACTION.INTERVIEW_SUMMARY_MONTH,
                TRIAL_ACTION.INTERVIEW_SUMMARY_QUARTER
            ];
            if (prepareStatusArr.includes(status) && summaryStatusArr.includes(dataStatus)) {
                return true;
            }
            if (logStatusArr.includes(status) && hasInterviewProcedure) {
                return true;
            }
            return false;
        },
        [status]
    );
    React.useEffect(() => {
        if (!data) {
            return;
        }
        const { opEmployeeCode = '', status: dataStatus, hasInterviewProcedure, assessComplete } = data;
        const { employeeCode } = user;
        const processCompleteFlag = assessComplete === 1;
        const interviewCompleteFlag = interviewComplete(dataStatus, hasInterviewProcedure);
        if (processCompleteFlag || interviewCompleteFlag) {
            const nextQuery = { id: data.detailId };
            if (!processCompleteFlag && interviewCompleteFlag) {
                nextQuery.status = status;
            }
            history.replace({
                pathname: '/office/personnelManagement/onboardAndLeave/informal/process/detail',
                query: nextQuery,
                ...sameTabProps
            });
            return;
        }

        if (!opEmployeeCode.includes(employeeCode)) {
            message.error('暂无权限！');
            history.push('/');
        }
    }, [JSON.stringify(data), JSON.stringify(user)]);

    const pageInfo = PAGE_INFO[status] || {};
    const Component = pageInfo.component;

    React.useEffect(() => {
        if (pageInfo.title) {
            dispatch({
                type: 'menu/updateTabTitle',
                payload: {
                    pathname: location.pathname,
                    title: pageInfo.title
                }
            });
        }
    }, [data]);

    if (!data?.status) {
        return (
            <PageContainer loading={loading}>
                <Empty />
            </PageContainer>
        );
    }

    return (
        <PageContainer loading={loading}>
            <PersonalCard data={data?.employeeInfo} />
            <Divider style={{ margin: '16px 0' }} />
            {Component && <Component data={data} />}
        </PageContainer>
    );
}

export default connect(({ user }) => ({ user: user.currentUser }))(Process);
